#!/user/bin/env python
# -*- coding:utf-8 -*-
# 作者：洛月
# 创建：2021-03-13 11:36
# 更新：2021-03-13 11:36
# 功能：
from selenium import webdriver
import time
import requests


def download(url):
    print(url)
    file = requests.get(url, headers={
        'Connection': 'keep-alive',
        'Pragma': 'no-cache',
        'Cache-Control': 'no-cache',
        'Upgrade-Insecure-Requests': '1',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36',
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
        'Accept-Language': 'zh-CN,zh;q=0.9',
    }, verify=False, stream=True)
    total = int(file.headers['Content-Length'])
    current = 0
    with open(f"get/{time.time()}.{url.split('.')[-1]}", 'wb') as fp:
        for chunk in file.iter_content(10240):
            current += len(chunk)
            fp.write(chunk)
            print(f'\r[{current}/{total} | {int(round(current / total, 2) * 100)}%]:{url}', end='')


driver = webdriver.Chrome('chromedriver.exe')
driver.maximize_window()
driver.get('http://www.jj20.com/bz/ktmh/dmrw/72611.html')
driver.implicitly_wait(10)
download(driver.find_element_by_css_selector('#bigImg').get_attribute('src'))
for i in range(130):
    try:
        driver.find_element_by_css_selector('#pageNext').click()
    except:
        driver.find_element_by_css_selector('body > div.wzfz.tu.mt2 > div.tu-xnext > span > a').click()
    driver.implicitly_wait(10)
    download(driver.find_element_by_css_selector('#bigImg').get_attribute('src'))
